<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\BuildType;
use App\Http\Requests\BuildTypeCreateRequest;
use App\Http\Requests\BuildTypeUpdateRequest;
class BuildTypeController extends Controller
{
    protected $fields = [
        'name' => '',
    ];
    
   public function index(Request $request){
       if($request->ajax()){
           $data = array();
           $data['draw'] = $request->get('draw');
           $start = $request->get('start');
           $length = $request->get('length');
           $order = $request->get('order');
           $columns = $request->get('columns');
           $search = $request->get('search');
           $data['recordsTotal'] = BuildType::count();
           if (strlen($search['value']) > 0) {
               $data['recordsFiltered'] = BuildType::where(function ($query) use ($search) {
                   $query->where('name', 'LIKE', '%' . $search['value'] . '%');
               })->count();
           
               $data['data'] = BuildType::where(function ($query) use ($search) {
                   $query->where('name', 'LIKE', '%' . $search['value'] . '%');
               })
               ->skip($start)->take($length)
               ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir'])
               ->get();
           
           }else{
               $data['recordsFiltered'] = BuildType::count();
               $data['data'] = BuildType::
               skip($start)->take($length)
               ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir'])
               ->get();
           
           }
           
           return response()->json($data);
       }
     return view('admin.buildtype.index');
   }
   

   public function show($id){
   
   }
   
   public function create(){
       $data = [];
       foreach ($this->fields as $field => $default) {
           $data[$field] = old($field, $default);
       }
       return view('admin.buildtype.create', $data);
   }
   
   public function store(BuildTypeCreateRequest $request)
   {
   
       $buildtype = new BuildType();
       foreach (array_keys($this->fields) as $field) {
           $buildtype->$field = $request->get($field);
       }
       $buildtype->save();
       return redirect('/admin/buildtype')->withSuccess('添加成功！');
   }
   
   public function edit($id)
   {
       $buildtype = BuildType::find((int)$id);
       if (!$buildtype) return redirect('/admin/buildtype')->withErrors("找不到该l类型!");
       $data = ['id' => (int)$id];
       foreach (array_keys($this->fields) as $field) {
           $data[$field] = old($field, $buildtype->$field);
       }
       return view('admin.buildtype.edit', $data);
   }
    
   /**
    * Update the specified resource in storage.
    *
    * @param PermissionUpdateRequest|Request $request
    * @param  int $id
    * @return \Illuminate\Http\Response
    */
   public function update(BuildTypeUpdateRequest $request, $id)
   {
       $buildtype = BuildType::find((int)$id);
       foreach (array_keys($this->fields) as $field) {
           $buildtype->$field = $request->get($field);
       }
       $buildtype->save();
       return redirect('admin/buildtype')->withSuccess('修改成功！');
   }
    
   public function destroy($id){
       $role = BuildType::find((int)$id);
       if ($role) {
           $role->delete();
       } else {
           return redirect()->back()
           ->withErrors("删除失败");
       }
        
       return redirect()->back()
       ->withSuccess("删除成功");
   }
   
}
